package com.fan.shop.businesslogic;

import java.util.Date;

import com.fan.shop.domain.Role;
import com.fan.shop.domain.User;
import com.fan.shop.global.ShopGlobalVariables;
import com.fan.shop.process.ProcExec;
import com.fan.shop.services.RoleService;
import com.fan.shop.services.UserService;

public class UserCreateProcess extends ProcExec<Integer> {

	private UserService userService;
	private RoleService roleService;
	private String username;
	private String password;
	private String email;
	private String address;
	private String postAddress;
	private String mobile;
	private String phone;

	private String roleName;
	private String roleGroup;

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public RoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getPostAddress() {
		return postAddress;
	}

	public void setPostAddress(String postAddress) {
		this.postAddress = postAddress;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getRoleName() {
		return roleName;
	}

	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}

	public String getRoleGroup() {
		return roleGroup;
	}

	public void setRoleGroup(String roleGroup) {
		this.roleGroup = roleGroup;
	}

	@Override
	public Integer processImpl() {

		if (userService.hasUser(username)) {
			setErrorMessage("User: " + username + " already exists!");
			return ShopGlobalVariables.FAILED;
		}
		User user = new User();
		user.setUsername(username);
		user.setPassword(password);
		user.setEmail(email);
		user.setAddress(address);
		user.setPostAddress(postAddress);
		user.setMobile(mobile);
		user.setPhone(phone);
		user.setRegisteredTime(new Date(System.currentTimeMillis()));
		user.setEnabled(1);

		userService.addUser(user);
		
		Role role = new Role();
		role.setUsername(username);
		role.setRoleName(ShopGlobalVariables.ROLE_USER);
		role.setRoleGroup(ShopGlobalVariables.GUEST);
		
		roleService.addRole(role);

		return ShopGlobalVariables.SUCCESS;
	}

}
